Ext.namespace("Milan.widget");
Milan.widget.MAX_ZINDEX=99;
Milan.widget.MessageBox=Class.create();
Milan.widget.MessageBox.prototype={
	initialize : function(_config,_id){
		this.id=(_id?_id:new Date().getTime());
		this.dialogContainerId = "dialogDiv";
		this.config = {
			title		: "提示：",
			width		: "auto",
			height		: "auto",
			boxClass	: "top_pop_tip",
			overlayClass: "masker",
			overflow	: "auto",
			maxOpacity	: 100,
			minOpacity	: 0,
			opacityStep	: 25,
			animateShow	: true,
			animateClose: true,
			speed		: 20,
			drag		: false,
			overlayShow	: false,
			closeBtnShow: false,
			closeClass	: "win-close floatr",
			content		: "",
			closeOther 	: true
		};
		this.config = Object.extend(this.config,_config ||{})
		this.createBox();
		if(this.config.drag){Milan.drag.init( this.box, this.box);}
		if(this.config.content){
			this.setContent(this.config.content);
		}
	},
	createBox:function(){
		if(this.config.closeOther)jQuery("."+this.config.boxClass).remove();
		this.createOverlay();
		this.box = document.createElement("DIV");
		this.box.id = this.id;
		this.box.className = this.config.boxClass;
		this.box.style.position="absolute";
		this.box.style.visibility="hidden";
		this.box.style.zIndex=(Milan.widget.MAX_ZINDEX++);
		if(this.config.width!="auto"){
			this.box.style.width=this.config.width+"px";
		}
		if(this.config.height!="auto"){
			this.box.style.height=this.config.height+"px";
		}
		this.box.style.overflow=this.config.overflow;
		this.box.appendChild(this._createBoxTitle());
		$(this.dialogContainerId).insert(this.box);
		this.createClose();
		return this;
	},
	_createBoxTitle:function(){
		var title = document.createElement("SPAN");
		title.innerHTML = this.config.title;
		return title;
	},
	createClose:function(){
		if(!this.config.closeBtnShow)return;
		this.closeBtn = document.createElement("A");
		this.closeBtn.id="box_close"+parseInt(Math.random()*10000);
		this.closeBtn.href ="javascript:void(0)";
		this.closeBtn.className=this.config.closeClass;
		Event.observe(this.closeBtn,'click',function(){this.close();}.bind(this));
		this.box.appendChild(this.closeBtn);
	},
	createOverlay:function(){//遮罩层
		if(!this.config.overlayShow)return;
		this.overlay=document.createElement("DIV");
		this.overlay.id = "overlay"+parseInt(Math.random()*10000);
		this.overlay.className = this.config.overlayClass;
		var bodyHeight = 0;//document.body.scrollHeight;
		var botHeight = $("bottomFrame").positionedOffset().top+$("bottomFrame").getHeight();
		this.overlay.style.height = ((bodyHeight>botHeight)?bodyHeight:botHeight)+"px";
		this.overlay.style.zIndex=(Milan.widget.MAX_ZINDEX++);
		if (Milan.navigator.isIE && Milan.navigator.version.indexOf("6")>-1)
		{
			var iframe = document.createElement("iframe");
			iframe.className = "iframe-masker";
			iframe.style.zIndex=(Milan.widget.MAX_ZINDEX++);
			this.overlay.appendChild(iframe);
			iframe.style.height = ((bodyHeight>botHeight)?bodyHeight:botHeight)+"px";
			this.overlay.iframe = iframe;
		}
		$(this.dialogContainerId).insert(this.overlay);
	},
	setHeight:function(height){
		this.box.style.height=height+"px";
		return this;
	},
	setWidth:function(width){
		this.box.style.width=width+"px";
		return this;
	},
	setPosition:function(x,y){
		x=x||20;
		this.box.style.top=x+"px";
		if(y){
			this.box.style.left=y+"px";
		}else{
			this.box.style.left = (screen.availWidth - parseInt($(this.box).getWidth()))/2 + "px";
		}
		return this;
	},
	show:function(){
		if(this.config.animateShow){
			this.setOpacity(this.box,this.config.minOpacity);
			this.box.style.visibility="visible";
			this.fadeIn(this.config.minOpacity);
		}else{
			this.box.style.visibility="visible";
		}
		this.setPosition();
		return this;
	},
	close:function(){
		if(jQuery("#"+this.id).length == 0)return;
		if(this.config.animateClose){
			this.fadeOut(this.config.maxOpacity);
		}else{
			if(this.config.overlayShow){
				$(this.overlay).remove();
			}
			$(this.box).remove();
		}
	},
	setContent:function(content){
		$(this.box).insert(content);
		this.setPosition();
		return this;
	},
	fadeIn: function(opacity) {
		if(opacity<this.config.maxOpacity){
			this.setOpacity(this.box,opacity);
			this.fadeInTimeout=setTimeout(function(){this.fadeIn(opacity+this.config.opacityStep);}.bind(this),this.config.speed)
		}else{
			this.setOpacity(this.box,this.config.maxOpacity);
			clearTimeout(this.fadeInTimeout);
		}
		return this;
	},
	fadeOut: function(opacity) {
		if(opacity>=this.config.minOpacity){
			this.setOpacity(this.box,opacity);
			this.fadeOutTimeout=setTimeout(function(){this.fadeOut(opacity-this.config.opacityStep);}.bind(this),this.config.speed)
		}else{
			$(this.box).remove();
			if(this.config.overlayShow){
				$(this.overlay).remove();
			}
			clearTimeout(this.fadeOutTimeout);
		}
		return this;
	},
	setOpacity:function(obj,opacity){
        if(!Milan.navigator.isIE){
        	obj.style.opacity = ((opacity >=1)?(opacity/100):opacity);
        }else{
        	obj.style.filter = "alpha(opacity:" + ((opacity <=1)?(opacity*100):opacity) + ")"; 
        }
	},
	setCloseTimer:function(duration){
		duration  = duration || 6;
		duration *=1000;
		var that = this;
		window.setTimeout(function(){
			if(jQuery("#"+that.id).length > 0){
				that.close();
			}
		},duration);
	}
};